package com.dlb.andorid.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.XServerMsg;
import com.dlb.andorid.bean.AddressRetrieval;
import com.dlb.andorid.bean.MyAddress;
import com.dlb.andorid.bean.Province;
import com.dlb.andorid.presenter.AddressPresenter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;
import com.kwan.xframe.util.DialogFactory;

import java.util.List;

public class AddressListActivity extends BaseCommonActivity implements XContract.IAddressView {

	private View tv_add;
	private AddressPresenter mPresenter;
	private RecyclerView mRecyclerView;
	private BaseQuickAdapter<MyAddress, BaseViewHolder> mAdapter;
	private List<MyAddress> mMyAddresses;

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new AddressPresenter(this);
		return mPresenter;
	}

	@Override
	protected void initData() {
		showProgress("");
		mPresenter.getAddressList();
	}

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_address_list;
	}

	@Override
	protected void initViews() {
		super.initViews();
		tv_add = findViewById(R.id.tv_add);
		tv_add.setOnClickListener(this);

		mRecyclerView = findViewById(R.id.recycleView);
		mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
		mRecyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		mRecyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		mRecyclerView.setHasFixedSize(true);

		//解决更新 闪烁
		mRecyclerView.setItemAnimator(null);
		mRecyclerView.setNestedScrollingEnabled(false);

		mAdapter = new BaseQuickAdapter<MyAddress, BaseViewHolder>(R.layout.list_item_my_address, mMyAddresses) {
			@Override
			protected void convert(BaseViewHolder helper, MyAddress item) {

				helper.setText(R.id.tv_province, item.getProvince() + item.getCity() + item.getArea());
				helper.setText(R.id.tv_address, item.getAreainfo());
				helper.setText(R.id.tv_name, item.getName() + item.getPhone());

				helper.addOnClickListener(R.id.tv_delete);
				helper.addOnClickListener(R.id.tv_edit);
			}
		};

		mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
			@Override
			public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {

				if (view.getId() == R.id.tv_delete) {

					View v = getLayoutInflater().inflate(R.layout.layout_common_dialog, null);
					final Dialog dialog = DialogFactory.showCustomDialog3(AddressListActivity.this, v);

					((TextView) v.findViewById(R.id.tv_msg)).setText("是否删除");

					v.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							dialog.dismiss();
							showProgress("");
							mPresenter.deleteAddress(mMyAddresses.get(position).getAddress_id());
						}
					});

					v.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							dialog.dismiss();
						}
					});


					dialog.show();


				} else if (view.getId() == R.id.tv_edit) {

					Bundle bundle = new Bundle();
					bundle.putInt("mode",AddressAddActivity.MODE_EDIT);
					bundle.putLong("id",mMyAddresses.get(position).getAddress_id());
					go2Activity(AddressAddActivity.class, bundle, false);
				}

			}
		});

		mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putSerializable("address", mMyAddresses.get(position));
				intent.putExtra("data", bundle);
				setResult(100, intent);
				finish();
			}
		});
		mRecyclerView.setAdapter(mAdapter);
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected String getTitleTxt() {
		return "我的收货地址";
	}

	@Override
	public String getPageName() {
		return null;
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == tv_add) {
			Bundle bundle = new Bundle();
			bundle.putInt("mode",AddressAddActivity.MODE_ADD);
			go2Activity(AddressAddActivity.class, bundle, false);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		mPresenter.getAddressList();
	}

	@Override
	public void addAddressSuccess(String msg) {

	}

	@Override
	public void getAddressCitySuccess(List<Province> data) {

	}

	@Override
	public void getAddressSuccess(List<MyAddress> data) {
		dismissProgress();
		mMyAddresses = data;
		mAdapter.setNewData(mMyAddresses);

	}

	@Override
	public void getAddressRetrievalSuccess(List<AddressRetrieval> data) {

	}

	@Override
	public void deleteAddSuccess(XServerMsg data) {
		mPresenter.getAddressList();
	}


}
